package hello.world;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity
{
    private GLSurfaceView mGLView = null;
    private final SpritesHolder spritesHolder = new SpritesHolder();
    private UpdateThread updateThread = null;
  
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
        if (updateThread == null)
        {
            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            updateThread = new UpdateThread(spritesHolder,
                new int2(metrics.widthPixels, metrics.heightPixels));
            new Thread(updateThread).start();
        }
        if (mGLView == null)
        {
            mGLView = new MainActivitySurfaceView(this, spritesHolder, updateThread);
            setContentView(mGLView);
        }
    }
    
    @Override
    protected void onPause()
    {
        super.onPause();
        updateThread.pause();
        mGLView.onPause();
    }
    
    @Override
    protected void onResume()
    {
        super.onResume();
        updateThread.unpause();
        mGLView.onResume();
    }
}
  
class MainActivitySurfaceView extends GLSurfaceView
{
    private final UpdateThread updateThread;
    
    public MainActivitySurfaceView(Context context, SpritesHolder spritesHolder,
        UpdateThread _updateThread)
    {
        super(context);
        updateThread = _updateThread;
        setRenderer(new HelloWorldRenderer(context, spritesHolder));
    }

    @Override 
    public boolean onTouchEvent(MotionEvent e)
    {
        return updateThread.onTouchEvent(e);
    }
}
